<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row>
				<div class="flex justify-between w-full mb8">
					<el-space>
						<el-form :inline="false" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef" label-width="10px">
							<el-row>
								<el-col :span="8" class="mb20">
									<el-form-item label="" prop="buildingId">
										<el-select v-model="state.queryForm.buildingId" filterable placeholder="请选择建筑物" :teleported="false">
											<el-option v-for="item in buildingOptions" :key="item.id" :label="item.buildingName" :value="item.id" />
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :span="8" class="mb20">
									<el-form-item label="" prop="floorName"> <el-input placeholder="请输入楼层" v-model="state.queryForm.floorName" /> </el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item>
										<el-button type="primary" @click="getDataList(true)">搜索</el-button>
										<el-button icon="refresh" @click="resetQuery()"></el-button>
									</el-form-item>
								</el-col>
							</el-row>
						</el-form>
					</el-space>
					<div>
						<el-button type="success" class="mr-2" @click="formDialogRef.openDialog()"> 新增</el-button>
						<!-- 更多操作 -->
						<el-popover :width="160">
							<template #reference>
								<el-button type="primary"
									>更多操作 <el-icon class="ArrowDown"><ArrowDown /></el-icon></el-button
							></template>
							<el-row>
								<el-col :span="24">
									<el-button
										class="ml-3"
										icon="edit-pen"
										text
										v-auth="'platform_basicPlaceFloor_edit'"
										:disabled="multiple || selectObjs.length !== 1"
										@click="formDialogRef.openDialog(selectObjs[0])"
										>编辑楼层</el-button
									>
									<el-button
										text
										type="danger"
										icon="DeleteFilled"
										@click="handleDelete(selectObjs)"
										v-auth="'platform_basicPlaceFloor_del'"
										:disabled="multiple"
										>删除楼层</el-button
									>
								</el-col>
							</el-row>
						</el-popover>
					</div>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="floorName" label="楼层" show-overflow-tooltip />
				<el-table-column prop="installCount" label="安装点数" show-overflow-tooltip />
				<el-table-column prop="buildingName" label="建筑物名称" show-overflow-tooltip />
				<el-table-column prop="planeFigure" label="平面图" show-overflow-tooltip />
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip />
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button icon="edit-pen" text type="primary" v-auth="'platform_basicPlaceFloor_edit'" @click="formDialogRef.openDialog(scope.row.id)"
							>编辑</el-button
						>
						<el-button icon="delete" text type="danger" v-auth="'platform_basicPlaceFloor_del'" @click="handleDelete([scope.row.id])">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
	</div>
</template>

<script setup lang="ts" name="systemBasicPlaceFloor">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/installation/basicPlaceFloor';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { fetchListAll as fetchBuildingListAll } from '/@/api/installation/basicPlaceBuilding';
// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
// 搜索变量
const queryRef = ref();
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);
const buildingOptions = ref<any[]>([]);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	createdIsNeed: false,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	state.queryForm.floorId = null;
	getDataList();
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};

const getBuildings = () => {
	fetchBuildingListAll().then((res) => {
		buildingOptions.value = res.data;
	});
};
getBuildings();

onMounted(() => {
	const params = history?.state?.params;
	params?.floorId && (state.queryForm.floorId = params?.floorId);
	params?.buildingId && (state.queryForm.buildingId = params?.buildingId);
	getDataList();
	history.state.params = undefined;
});
</script>
